// 给提交按钮绑定点击事件
window.onload = function(){
document.getElementById("submit").onclick = function(){
  if(checkUsername() && checkPassword() && checkRePassword() && checkPhone()){
      var flag = confirm("是否返回登录页面");
      if(flag){
          window.location.href = "../page/indext.html"
      }
  }else{
      alert("注册信息，格式有误，请修改")
  }
  return false;
}
// 绑定事件
document.getElementById("username").onblur = checkUsername;
document.getElementById("password").onblur = checkPassword;
document.getElementById("repassword").onblur = checkRePassword;
document.getElementById("phone").onblur = checkPhone;
// document.getElementById("code").onblur = checkCode;
}

// 用户名验证：
function checkUsername(){
    var username = document.getElementById("username").value;
    var reg_username = /^[a-zA-Z0-9_-]{4,16}$/;
    var flag = reg_username.test(username);
    var s_username = document.getElementById("s_username");
    if(flag){
        s_username.style.color="green";
        s_username.innerHTML = "正确";
    }else{
        if(username==""){
            s_username.style.color="red";
            s_username.innerHTML = "用户名不能为空";
        }else{
            s_username.style.color="red";
			s_username.innerHTML = "用户名格式有误";
        }
    }
    return false;
}
//密码验证：
function checkPassword(){
    var password = document.getElementById("password").value;//获取
    var reg_password = /^[a-zA-Z0-9_-]{8,16}$/;//正则表达式
    var flag = reg_password.test(password);//判断是否符合正则的规则
    var s_password = document.getElementById("s_password");//提示信息
    if(flag){
        s_password.style.color = "green";
        s_password.innerHTML = "正确"; 
    }else{
        if(password==""){
            s_password.style.color = "red";
            s_password.innerHTML = "密码不能为空";
        }else{
            s_password.style.color = "red";
            s_password.innerHTML = "密码格式错误";
        }
    }
    return false;
}

// 确认密码验证:
function checkRePassword (){
    var password = document.getElementById("password").value;
    var repassword = document.getElementById("repassword").value;
    var flag = false;
    if(repassword!==""&&password==repassword){
        s_repassword.style.color = "green";
        s_repassword.innerHTML = "正确";
        flag=true;
    }else{
        if(repassword==""){
            s_repassword.style.color = "red";
            s_repassword.innerHTML = "密码不能为空";
        }else{
            s_repassword.style.color = "red";
            s_repassword.innerHTML = "两次密码输入不一致";           
        }
        flag = false;
    }
    return flag;
}
// 手机号验证码：
function checkPhone(){
    var phone = document.getElementById("phone").value;
    var reg_phone = /^\d{11}$/;
    var flag = reg_phone.test(phone);
    var s_phone = document.getElementById("s_phone");
    if(flag){
       s_phone.style.color="green";
       s_phone.innerHTML = "正确";
    }else{
        if(phone==""){
            s_phone.style.color="red";
            s_phone.innerHTML = "手机号码不能为空";
        }
        else{
            s_phone.style.color="red";
            s_phone.innerHTML = "手机号码长度不为11位，请重新输入";
        }
    }
    return flag;
}

// // 验证码验证：
// function checkCode(n){
//     var code = document.getElementById("code").value;
//     var str = "";
//     for(var i=0;i<n;i++){
//     if((code>57&&code<65)||(code>90&&code<97)){
//         i--
//     }else{
//         var char = String.fromCharCode(code);
//         str+=char;
//     }
//     }
//     return str;
// }

